import {
	hideLoading,
	showLoading
} from './index.js'
import useUserStore from '@/store/user.js'

const file = (url, files,name) => {
	showLoading("正在上传...") // 显示上传进度的加载提示
	const userStore = useUserStore();
	const token = userStore.token;
	const header = token ? {
		"Authorization": `Bearer ${token}`,
	} : {}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `http://123.162.182.213:22541/${url}`,
			name: name,
			file: files,
			header,
			success: (res) => {
				console.log("上传成功-->", res)
				resolve(JSON.parse(res.data)) // 上传成功时，解决Promise
			},
			fail: (error) => {
				console.error("上传失败-->", error)
				reject(error) // 上传失败时，拒绝Promise
			},
			complete: () => {
				hideLoading() // 无论成功失败，隐藏加载提示
			}
		})
	})
}

export default file;